#include "csv2sql.h"

namespace csv2sql
{

  Options::Options()
  {
    applyDefaults();
  }
  
  Options::Options(int argc, char **argv)
  {
    applyDefaults();
    
    if (argc < 2)
    {
      printf("\nPlease specify a CSV input file.\n\n");
      isOk =    false;
      return;
    }
    if (argc == 2)
    {
      file = argv[1];
      return;
    }
    // Read command line parameters
    for (int i=2; i<argc; i++) 
    {
      if (!strcmp("-col-end", argv[i-1]) )
      {
        col_end = argv[i];
      }
      else if (!strcmp("-row-end", argv[i-1]) )
      {
        row_end = argv[i];
      }
      else if (!strcmp("-quote", argv[i-1]) )
      {
        quote = argv[i];
      }
      else if (!strcmp("-escape", argv[i-1]) )
      {
        escape = argv[i];
      }
      else if (!strcmp("-table", argv[i-1]) )
      {
        table = argv[i];
      }
      else if (argv[i][0] != '-')
      {
        file = argv[i];
        break;
      }
    }
  }
  
  void Options::applyDefaults()
  {
    isOk =    true;
    file =    (char *)"";
    col_end = (char *)",";
    row_end = (char *)"\r\n";
    quote =   (char *)"\"\'";
    escape =  (char *)"\\";
    table =   (char *)"MyTable";
  }
  
  bool Options::IsOk() { return isOk; }
}
